<?php
session_start();
$user=$_SESSION['JID']; //Tova ime go polzvam za da znam na koi potrebitel se kachva snimkata
$a=rand(0, 9999999999999999);
$b=rand(0, 9999999999999999);
$namePic=$a.$b;

$namePic=$user.$namePic.".jpg"; //Tova e imeto na snimkata koeto e sastaveno ot imeto na potrebitelq
$miniPic=$namePic."mini".".jpg";

if (!$_FILES['uploaded_file']['tmp_name']) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
//sleep(5);
//header("Location: settings.php");
exit();
}
else{
if (getimagesize($_FILES['uploaded_file']['tmp_name'])) //Tyk pravim proverka dali shte varne rezultat funkciqta getimagesize ako vurne false zna4i ne e kartinka ina4e dava razmera na kartinkata
{
mysql_connect("localhost", "sapmenet_admin", "a1s2d3f4g5h6j7") or die ("Problem with datebase");
mysql_select_db("sapmenet_users"); //Bazata danni tyka
$mainPic=mysql_query("SELECT mainPic FROM users WHERE ID={$user}");
$mainPic=mysql_result($mainPic, 0);
if ($mainPic!='asd')
{
	unlink($mainPic);
}
mysql_query("UPDATE `users` SET `mainPic` = 'pictures/$user/$namePic' WHERE `ID` = '$user'");

move_uploaded_file($_FILES['uploaded_file']['tmp_name'], "Bigpictures/$namePic"); //Kachva kartinkata v papka images sas imeto koeto sme zadali po gore

$resized=$namePic;//Imeto na resizanatata kartinka
$resizedMini=$miniPic;

$input_image="Bigpictures/$namePic"; //Putq do originala !!
$size = getimagesize( $input_image );//Tyk vzimame razmera na originalnata kartinka
$thumb_width = "150"; //Shirinata na resizanatata
$thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] ); //Tyk s tezi iz4isleniq zapazvame proporciqta shirina/duljina
$thumbnail = ImageCreateTrueColor( $thumb_width, $thumb_height );
$src_img = ImageCreateFromJPEG( $input_image );
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1] );
ImageJPEG( $thumbnail, "pictures/$user/$resized" ); //Tova e kade da zapameti resizanatata snimka
ImageDestroy( $thumbnail );
//malka kartinka
$size = getimagesize( $input_image );//Tyk vzimame razmera na originalnata kartinka
$thumb_width = "120"; //Shirinata na resizanatata
$thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] ); //viso4inata na resiznatata
$thumbnail = ImageCreateTrueColor( $thumb_width, $thumb_height );
$src_img = ImageCreateFromJPEG( $input_image );
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1] );
ImageJPEG( $thumbnail, "pictures/$user/$resizedMini" ); //Tova e kade da zapameti resizanatata snimka
ImageDestroy( $thumbnail );
unlink($input_image);
header("Location: http://sapme.net/Framework/profilePage.php?id=".$_SESSION['JID']."");
}
else
{
echo "You can upload only pictures !";
}
}
?>

